package fileReader.demo.reader;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.geom.Point2D;
import java.util.Vector;

public class PolygonMapLayer extends MapLayer{

	/**
	 * Draws this generarea map layer on screen
	 * 
	 * @param g graphics
	 * @param zoom value
	 * @param xtrans x_translation
	 * @param ytrans y_translation
	 */
	public void drawLayer(Graphics g, double zoom, int x_translation, int y_translation, String layer) {
		if(layer.contains("deptharea")){
			for (int i = 0 ; i < data.size() ; i++) 
			 {
				g.setColor(Color.BLACK); //
				PolygonDrawer p = (PolygonDrawer) data.get(i);
				p.Draw(g,zoom,x_translation, y_translation, layer);

	    		for(int a=0; a<p.getHoleList().size(); a++)
	    		{
	    			PolygonDrawer pp = (PolygonDrawer)p.getHoleList().get(a);
	    			pp.Draw(g,zoom,x_translation, y_translation, layer);
	    		}
			 }
		}else {
			for (int i = 0 ; i < data.size() ; i++) 
			 {
				 PolygonDrawer p = (PolygonDrawer) data.get(i);
				 p.Draw(g,zoom,x_translation, y_translation, layer);
			 }
		}
		
	}

	/**
	 * This method reduces data from the map layer.
	 * 
	 * @param points approximate number of verteces to allow
	 */
	public void reduceData(int points) {
		int vertexCount=0;
		double nd=0;
		int n=0;
		
		 for (int i = 0 ; i < data.size() ; i++) 
		 {
			DepthArea p = (DepthArea) data.get(i);
			if(p.getMinDepth()>=Simulator.getSelectedDepth()){
				vertexCount+=p.getVertexList().size();
			}
		 
			for(int a=0; a<p.getHoleList().size(); a++)
			{
				DepthArea pp = (DepthArea)p.getHoleList().get(a);
				if(pp.getMinDepth()>=Simulator.getSelectedDepth()){
					for(int h=0;h<pp.getHoleList().size();h++){
						vertexCount+=((PolygonDrawer)pp.getHoleList().get(h)).getVertexList().size();
					}
				}
			}
		 }
		 System.out.println("Vahennetaan: " +points);
		 System.out.println("verc "+vertexCount);
	    nd=(double)vertexCount/(double)points;
	    n = (int)nd;
	    System.out.println("pois kaikki paitsi joka "+n);
	    
				
		for(int j=0; j<data.size(); j++)
		{
			if(((PolygonDrawer)data.elementAt(j)).getVertexList().size()<(3*n))
				n = n/2;
			
			if(nd-n>0.5 || n<0.5)
	        	n = n+1;
			
			((PolygonDrawer)data.elementAt(j)).reduceData(n);
		}
		
		int vertexC = 0;
		for (int i = 0 ; i < data.size() ; i++) 
		 {
			DepthArea p = (DepthArea) data.get(i);
			if(p.getMinDepth()>=Simulator.getSelectedDepth()){
				vertexC+=p.getVertexList().size();
			}
		 
		for(int a=0; a<p.getHoleList().size(); a++)
		{
			DepthArea pp = (DepthArea)p.getHoleList().get(a);
			if(pp.getMinDepth()>=Simulator.getSelectedDepth()){
				for(int h=0;h<pp.getHoleList().size();h++){
					vertexC+=((PolygonDrawer)pp.getHoleList().get(h)).getVertexList().size();
				}
			}
		}
		 }
		
		System.out.println("verc "+vertexC);
	}

	/**
	 * This method calculates a good quess for hole depth values.
	 * Method is based on an assumption that signsounds inside a depth area
	 * should only have values between minimum and maximum depths of depth area
	 * 
	 * @param signsoundData depths are derived from signsound data
	 */
	public void calcHoleDepths(Vector signSoundData) {
	
		Point2D cont = new Point2D.Double(0,0);
		
		for(int i=0; i<data.size(); i++)
		{
			PolygonDrawer p = ((PolygonDrawer)data.get(i));
			for(int j=0; j<p.getHoleList().size(); j++)
			{
				DepthArea hole = ((DepthArea)p.getHoleList().get(j));
				
				boolean depthFound = false;
				int n=0;
				
				while(!depthFound && n<signSoundData.size())
				{
	
					PointDrawer control = (PointDrawer)signSoundData.get(n);
					cont.setLocation(control.getX(),control.getY());
	
					if(hole.Inside(cont))
					{	
	
						if(control.getDepth()>50.0)
							hole.setMinDepth(50.0);
						else if(control.getDepth()>20.0)
						{
							hole.setMinDepth(20.0);
							hole.setMaxDepth(50.0);
						}
						else if(control.getDepth()>10.0)
						{
							hole.setMinDepth(10.0);
							hole.setMaxDepth(20.0);
						}
						else if(control.getDepth()>0.0)
						{
							hole.setMinDepth(0.0);
							hole.setMaxDepth(10.0);
						}
						depthFound = true;
					}
					n++;
				}//while
			}//for
		}//for
	}//method

}
